package com.itheima.controller;

import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.vo.SetmealVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ：yuanjian
 * @date ：Created in 2020/11/15 21:29
 * @description：
 */
@RestController
public class SetmealMobileControler {

    @Reference
    private SetmealService setmealService;

    @Resource
    private RedisTemplate redisTemplate;
    /**
     * 查询所有套餐
     *
     * @return
     */
    @GetMapping("setmeal/findAll")
    public Result findSetmealAll() {
        List<Setmeal> setmealList = redisTemplate.opsForHash().values("com:itheima:pojo:setmeal");
        if(setmealList.size()>0){
            return new Result(setmealList);
        }else {
            setmealList  = setmealService.findSetmealAll();
            return new Result(setmealList);
        }

    }

    /**
     * 通过id查询套餐和检查组以及检查项数据
     *
     * @return
     */
    @GetMapping("setmeal/findSetmealDetailById/{id}")
    public Result findSetMealDetail(@PathVariable("id") Long id) {
        SetmealVo setmealVo = (SetmealVo) redisTemplate.opsForValue().get("com:itheima:pojo:setmealVo"+id);
        if (setmealVo != null){
            return new Result(setmealVo);
        }else {
            setmealVo = setmealService.findSetMealDetail(id);
            return new Result(setmealVo);
        }
    }

    @GetMapping("/setmealMobile/findById")
    public Result findById(Long id){
        return new Result(setmealService.getById(id));
    }
}
